//
//  HOAppraiseListViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 2/12/16.
//  Copyright © 2016 ShenOu. All rights reserved.
//

#import "HOAppraiseListViewController.h"
#import "HOAppraiserTableViewCell.h"
#import "HOConstants.h"
#import "HOUIMgr.h"

@interface HOAppraiseListViewController ()<HOAppraiserTableViewCellDelegate>
{
	NSMutableArray *appraiseList;
}
@end

@implementation HOAppraiseListViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[super initTableViewWithPullUp];
	[self initDataSource];
//	[self initTestData];
	[self initNoDataView];
	[self getAppraiseEntityWithMaxId:1];
}

- (void)initDataSource
{
	appraiseList = [[NSMutableArray alloc] init];
}

- (void)sendComplaint:(int)appraiseId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetAppraiseList,
										kHO_FIELD_UserId:[NSNumber numberWithInt:self.userId]}];
}
- (void)initTestData
{
	for (int i = 0; i < 10; i ++) {
		HOAppraiseEntity *entity = [[HOAppraiseEntity alloc] init];
		//	NSDictionary *appraiseDic = [dic objectForKey:kHO_FIELD_AppraiseInfo];
		entity.appraiseId = 3;
		entity.realName = @"zhimafox";
		entity.time = 1434567898334;
		entity.content = @"非常不错的翻译师";
		entity.value = 3;
//		entity.iconURL = @"http://img0.imgtn.bdimg.com/it/u=1482009946,1606111286&fm=21&gp=0.jpg";
		[appraiseList addObject:entity];
		[entity release];
	}
}

- (void)dealloc
{
	[appraiseList removeAllObjects];
	[appraiseList release];
	[super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"评价", @"评价")];
}

- (void)getAppraiseEntityWithMaxId:(int)MaxId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetAppraiseList,
										kHO_FIELD_MaxID:[NSNumber numberWithInt:MaxId],
										kHO_FIELD_UserId:[NSNumber numberWithInt:self.userId]}];
}

- (void)pullUpToRefresh
{
	HOAppraiseEntity *entity = [appraiseList lastObject];
	[self getAppraiseEntityWithMaxId:entity.appraiseId];
}

- (void)pullDownToRefresh
{
	[self getAppraiseEntityWithMaxId:1];
}

- (NSInteger)getListCount
{
	return [appraiseList count];
}

//- (UILabel *)getHeaderLblWithTitle:(NSString *)title
//{
//	UILabel *headerLbl = [[[UILabel alloc]
//						   initWithFrame:CGRectMake(0,
//													50 / 2,
//													self.view.frame.size.width / 2,
//													22)] autorelease];
//	headerLbl.textAlignment = NSTextAlignmentRight;
//	headerLbl.text = title;
//	headerLbl.textColor = MAIN_COLOR_NORMAL;
//	return headerLbl;
//}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//	UIView *view = [[[UIView alloc]
//					 initWithFrame:CGRectMake(0,
//											  0,
//											  tableView.frame.size.width,
//											  [self tableView:tableView heightForHeaderInSection:section])] autorelease];
//	
//	UILabel *headerLbl = [self getHeaderLblWithTitle: NSLocalizedString(@"综合评价", @"综合评价")];
//	headerLbl.frame = CGRectMake(0,
//								 (view.frame.size.height - 22) / 2,
//								 tableView.frame.size.width / 2 - 5,
//								 22);
//	headerLbl.textColor = MAIN_COLOR_NORMAL;
//	//	headerLbl.layer.borderWidth = 1;
//	headerLbl.font = [UIFont systemFontOfSize:CELL_FONT_SIZE - 1];
//	[view addSubview:headerLbl];
//	
//	RatingBar *ratingBar = [[RatingBar alloc]
//							initWithFrame:CGRectMake(headerLbl.frame.size.width + headerLbl.frame.origin.x + HO_HORIZONAL_PADDING,
//													 headerLbl.frame.origin.y,
//													 90,
//													 8)  isSmall:NO];
//	//	ratingBar.layer.borderWidth = 1;
//	ratingBar.starNumber = self.appraise;
//	[view addSubview:ratingBar];
//	ratingBar.frame = CGRectMake(ratingBar.frame.origin.x,
//								 (view.frame.size.height - ratingBar.frame.size.height) / 2,
//								 ratingBar.frame.size.width,
//								 ratingBar.frame.size.height);
//	[ratingBar release];
//	
//	return view;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//	return 50;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOAppraiserTableViewCell *cell = (HOAppraiserTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
	CGFloat height = [cell getCellHeight];
	return height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count = [appraiseList count];
	[self updateNoResultView:count];
	return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *HOAppraiserTableViewCellIdentifier = @"HOAppraiserTableViewCellIdentifier";
	
	HOAppraiserTableViewCell *cell = (HOAppraiserTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOAppraiserTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOAppraiserTableViewCell alloc]
										   initWithStyle:UITableViewCellStyleDefault
										   reuseIdentifier: HOAppraiserTableViewCellIdentifier] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		[cell initWithWidth:baseTableview.frame.size.width];
	}
	HOAppraiseEntity *entity = [appraiseList objectAtIndex:indexPath.row];
	[cell setEntity:entity];
	return cell;
}

#pragma mark
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response.action isEqualToString:kHO_ACTION_GetAppraiseList])
	{
		int maxId = [[[response.baseRequest params] objectForKey:kHO_FIELD_MaxID] intValue];
		if(maxId == 0)
		{
			[appraiseList removeAllObjects];
		}
		NSArray *appraises = response.dataNode;
		for(int i = 0; i < [appraises count]; i ++)
		{
			NSDictionary *dic = [appraises objectAtIndex:i];
			HOAppraiseEntity *entity = [self getAppraiseEntityWithId:[[dic objectForKey:kHO_FIELD_AppraiseId] intValue]];
			[entity parseDic:dic];
			if(![appraiseList containsObject:entity])
				[appraiseList addObject:entity];
		}
		[baseTableview reloadData];
		[self doneLoadingTableViewRefresh];
	}
}

- (HOAppraiseEntity *)getAppraiseEntityWithId:(int)Id
{
	HOAppraiseEntity *entity = [entityDic objectForKey:[NSNumber numberWithInt:Id]];
	if(entity == nil)
	{
		entity = [[[HOAppraiseEntity alloc] init] autorelease];
		[entityDic setObject:entity forKey:[NSNumber numberWithInt:Id]];
		entity.appraiseId = Id;
	}
	return entity;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
